package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.core.validate.AddGroup;
import com.wande.common.core.validate.EditGroup;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.FillDataBo;
import com.wande.dataplatform.domain.bo.FillTemplateBo;
import com.wande.dataplatform.domain.vo.FillDataVO;
import com.wande.dataplatform.domain.vo.FillTemplateVO;
import com.wande.dataplatform.service.IFillDataService;
import com.wande.dataplatform.service.IFillTemplateService;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据填报控制器
 *
 * @author wande
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataplatform/fill")
public class DataFillController extends BaseController {

    private final IFillTemplateService fillTemplateService;
    private final IFillDataService fillDataService;

    // ==================== 模板管理 ====================

    /**
     * 查询数据填报模板列表
     */
    @SaCheckPermission("dataplatform:fill:template:list")
    @GetMapping("/template/list")
    public TableDataInfo<FillTemplateVO> templateList(FillTemplateBo bo, PageQuery pageQuery) {
        return fillTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取数据填报模板详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dataplatform:fill:template:query")
    @GetMapping("/template/{id}")
    public R<FillTemplateVO> getTemplateInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(fillTemplateService.queryById(id));
    }

    /**
     * 根据编码获取模板
     *
     * @param code 模板编码
     */
    @SaCheckPermission("dataplatform:fill:template:query")
    @GetMapping("/template/code/{code}")
    public R<FillTemplateVO> getTemplateByCode(@PathVariable String code) {
        return R.ok(fillTemplateService.queryByCode(code));
    }

    /**
     * 新增数据填报模板
     */
    @SaCheckPermission("dataplatform:fill:template:add")
    @Log(title = "数据填报模板", businessType = BusinessType.INSERT)
    @PostMapping("/template")
    public R<Long> addTemplate(@Validated(AddGroup.class) @RequestBody FillTemplateBo bo) {
        return R.ok(fillTemplateService.insertByBo(bo));
    }

    /**
     * 修改数据填报模板
     */
    @SaCheckPermission("dataplatform:fill:template:edit")
    @Log(title = "数据填报模板", businessType = BusinessType.UPDATE)
    @PutMapping("/template")
    public R<Void> editTemplate(@Validated(EditGroup.class) @RequestBody FillTemplateBo bo) {
        return toAjax(fillTemplateService.updateByBo(bo));
    }

    /**
     * 删除数据填报模板
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dataplatform:fill:template:remove")
    @Log(title = "数据填报模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/template/{ids}")
    public R<Void> removeTemplate(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(fillTemplateService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导出模板
     *
     * @param id 模板ID
     * @param format 格式
     */
    @SaCheckPermission("dataplatform:fill:template:export")
    @Log(title = "数据填报模板", businessType = BusinessType.EXPORT)
    @GetMapping("/template/export/{id}")
    public R<byte[]> exportTemplate(@PathVariable Long id, @RequestParam(defaultValue = "json") String format) {
        return R.ok(fillTemplateService.exportTemplate(id, format));
    }

    /**
     * 导入模板
     *
     * @param data 模板数据
     * @param format 格式
     */
    @SaCheckPermission("dataplatform:fill:template:import")
    @Log(title = "数据填报模板", businessType = BusinessType.IMPORT)
    @PostMapping("/template/import")
    public R<Void> importTemplate(@RequestBody byte[] data, @RequestParam(defaultValue = "json") String format) {
        return toAjax(fillTemplateService.importTemplate(data, format));
    }

    // ==================== 填报数据管理 ====================

    /**
     * 查询数据填报数据列表
     */
    @SaCheckPermission("dataplatform:fill:data:list")
    @GetMapping("/data/list")
    public TableDataInfo<FillDataVO> dataList(FillDataBo bo, PageQuery pageQuery) {
        return fillDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取数据填报数据详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dataplatform:fill:data:query")
    @GetMapping("/data/{id}")
    public R<FillDataVO> getDataInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(fillDataService.queryById(id));
    }

    /**
     * 新增数据填报数据
     */
    @SaCheckPermission("dataplatform:fill:data:add")
    @Log(title = "数据填报数据", businessType = BusinessType.INSERT)
    @PostMapping("/data")
    public R<Long> addData(@Validated(AddGroup.class) @RequestBody FillDataBo bo) {
        return R.ok(fillDataService.insertByBo(bo));
    }

    /**
     * 修改数据填报数据
     */
    @SaCheckPermission("dataplatform:fill:data:edit")
    @Log(title = "数据填报数据", businessType = BusinessType.UPDATE)
    @PutMapping("/data")
    public R<Void> editData(@Validated(EditGroup.class) @RequestBody FillDataBo bo) {
        return toAjax(fillDataService.updateByBo(bo));
    }

    /**
     * 删除数据填报数据
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dataplatform:fill:data:remove")
    @Log(title = "数据填报数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/data/{ids}")
    public R<Void> removeData(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(fillDataService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 提交填报数据
     *
     * @param id 数据ID
     */
    @SaCheckPermission("dataplatform:fill:data:submit")
    @Log(title = "提交填报数据", businessType = BusinessType.UPDATE)
    @PostMapping("/data/submit/{id}")
    public R<Void> submitData(@PathVariable Long id) {
        return toAjax(fillDataService.submitData(id));
    }

    /**
     * 审批填报数据
     *
     * @param id 数据ID
     * @param approved 是否通过
     * @param reason 原因
     */
    @SaCheckPermission("dataplatform:fill:data:approve")
    @Log(title = "审批填报数据", businessType = BusinessType.UPDATE)
    @PostMapping("/data/approve/{id}")
    public R<Void> approveData(@PathVariable Long id, 
                                @RequestParam Boolean approved, 
                                @RequestParam(required = false) String reason) {
        return toAjax(fillDataService.approveData(id, approved, reason));
    }

    /**
     * 验证填报数据
     *
     * @param templateId 模板ID
     * @param dataContent 数据内容
     */
    @SaCheckPermission("dataplatform:fill:data:validate")
    @PostMapping("/data/validate")
    public R<Boolean> validateData(@RequestParam Long templateId, @RequestBody String dataContent) {
        return R.ok(fillDataService.validateData(templateId, dataContent));
    }

    /**
     * 导出填报数据
     *
     * @param id 数据ID
     * @param format 格式
     */
    @SaCheckPermission("dataplatform:fill:data:export")
    @Log(title = "数据填报数据", businessType = BusinessType.EXPORT)
    @GetMapping("/data/export/{id}")
    public R<byte[]> exportData(@PathVariable Long id, @RequestParam(defaultValue = "excel") String format) {
        return R.ok(fillDataService.exportData(id, format));
    }

    /**
     * 导入填报数据
     *
     * @param templateId 模板ID
     * @param data 数据
     * @param format 格式
     */
    @SaCheckPermission("dataplatform:fill:data:import")
    @Log(title = "数据填报数据", businessType = BusinessType.IMPORT)
    @PostMapping("/data/import")
    public R<Void> importData(@RequestParam Long templateId, 
                               @RequestBody byte[] data, 
                               @RequestParam(defaultValue = "excel") String format) {
        return toAjax(fillDataService.importData(templateId, data, format));
    }
}
